# coding=utf-8
import logging, time, os


class Logger(object):
    '''定义Logger类'''

    def __init__(self, logger_name):
        '''
        指定保存日志的文件路径，日志级别，以及调用文件
        将日志存入到指定的文件中
        '''
        # 创建一个logger
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.DEBUG)  # 设置收集器的级别
        # 设置日志的存放路径及日志文件名称
        self.rq = time.strftime('%Y%m%d', time.localtime(time.time()))
        self.log_path = os.path.dirname(__file__) + '/../log/'
        log_name = self.log_path + self.rq + '.log'

        # 创建一个handler，用于写入日志文件
        fh = logging.FileHandler(log_name,'a',encoding='utf-8')
        fh.setLevel(logging.INFO)  # 设置输出渠道的级别
        # 输出的内容是从收集器获取的，等级要大于等于收集器等级，如果收集器等级是INFO，handdler是DEGBU,日志是不会被输出的，收集器根本没收集到


        # 再创建一个handler，用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter('%(asctime)s - %(filename)s - %(funcName)s - %(levelname)s - %(message)s')
        #将定义好的输出形式添加到handler
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        # 为logger实例增加一个处理器
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

        # 避免日志重复输出,在记录日志之后移除句柄
        # self.logger.removeHandler(fh)
        # self.logger.removeHandler(ch)

        # 关闭文件
        fh.close()
        ch.close()

    def getlog(self):
        return self.logger

# class Log():
#     def __init__(self):
#         curTime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
#         self.logname = os.path.dirname(__file__) + '/../log/' + curTime + '.log'
#
#     def getlogger(self, level, msg):
#
#         # 创建一个logger
#         logger = logging.getLogger()
#         logger.setLevel(logging.DEBUG) #设置收集器的级别
#
#         # 创建一个handler，用于输出日志到文件
#         fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')
#         fh.setLevel(logging.INFO) #设置输出渠道的级别
#         # 输出的内容是从收集器获取的，等级要大于等于收集器等级，如果收集器等级是INFO，handdler是DEGBU,日志是不会被输出的，收集器根本没收集到
#
#         # 创建一个handler，用于输出日志到控制台
#         ch = logging.StreamHandler()
#         ch.setLevel(logging.INFO)
#
#         # 定义handler的输出格式
#         formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#         fh.setFormatter(formatter)
#         ch.setFormatter(formatter)
#
#         # 给logger添加handler
#         logger.addHandler(fh)
#         logger.addHandler(ch)
#
#         # 记录日志
#         if level == 'info':
#             logger.info(msg)
#         elif level == 'debug':
#             logger.debug(msg)
#         elif level == 'warning':
#             logger.warning(msg)
#         elif level == 'error':
#             logger.error(msg)
#
#         # 避免日志重复输出,在记录日志之后移除句柄
#         logger.removeHandler(fh)
#         logger.removeHandler(ch)
#
#         # 关闭日志文件
#         fh.close()
#
#     def log_debug(self, msg):
#         self.getlogger('debug', msg)
#
#     def log_info(self, msg):
#         self.getlogger('info', msg)
#
#     def log_warning(self, msg):
#         self.getlogger('warning', msg)
#
#     def log_error(self, msg):
#         self.getlogger('error', msg)
